<?php
/**
 * URI Rule
 * Validates that a value is a valid URI.
 * 
 * @package     Q-Validate
 * @subpackage  Rules
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @todo        Unit tests
 * @todo        Find the best possible pattern for this
 * @version     $Id$
 */
class Visi_Rule_Uri extends Q_Rule_Abstract {

    const PATTERN_HOST = '([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])';
    
    const PATTERN_PATH = '(/[^\s\?]+)+';
    
    const PATTERN_SCHEME = '[a-zA-Z0-9]+';
    
    const PATTERN_QUERY = '?([^=]=[^&]&?)+';
    
    /**
     * Parameters
     * scheme - if specified, will require that the URI has a specific scheme
     *          if set to false, scheme will not be required at all
     *          can also be an array of allowed schemes
     *          can also just be "true" to ensure there is a scheme (but doesn't matter what it is)
     * path   - if set to false, uri cannot have a path
     * query  - if set to true, query is required
     *          if set to false, query is not allowed
     *          if set to null, query is optional
     */
    protected $_params = array(
        'scheme' => true,
        'path' => null,
        'query' => null,
    );
    
    /**
     * @todo Finish this later
     */
    public function isValid($value) {
    
        return true;
        $schemeParam = $this->getParam('scheme');
        $pathParam = $this->getParam('path');
        $queryParam = $this->getParam('query');
        $host = self::PATTERN_HOST;
        $path = "";
        $scheme = "";
        $query = "";
        if ($schemeParam === true) {
            $scheme .= self::PATTERN_SCHEME . '://';
        } elseif ($schemeParam === false) {
            $scheme .= "";
        } elseif (is_array($schemeParam)) {
            $scheme .= '(' . implode('|', $schemeParam) . ')://';
        } else {
            $scheme .= (string) $schemeParam . '://';
        }
        if ($pathParam === false) {
            
        } else {
            $path .= self::PATTERN_PATH;
        }
        if ($queryParam == true) {
            $query .= self::PATTERN_QUERY;
        } elseif ($queryParam == false) {
            // do nothing
        }
        $pattern = "#^({$scheme})({$host})({$path})({$query})$#";
        return $this->_validatePattern($pattern, $value);
    
    }

}
